<?php

namespace app\index\controller;

class CustomerBase
{
    public function index()
    {
        $query = \app\model\Customer::field(['customer.id', 'company_name', 'customer_follow_id'])->scope('user')->with(['follow'])->order('id', 'desc');

        $query->where('sales_id', request()->user->id);

        if (request()->has('provinceId')) {
            $query->where('province_id', input('provinceId'));
        }

        if (request()->has('companyName')) {
            $query->whereLike('company_name', '%' . input('companyName') . '%');
        }

        if (request()->has('statusValue')) {
            $query->hasWhere('follow', ['status_value' => input('statusValue')]);
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['customers' => $models]]);
    }

    public function create()
    {
        $param = request()->param();

        $param['salesId'] = 1;

        $model = \app\model\Customer::create($param);

        if (!$model) {
            return json(['code' => 40400, 'msg' => '新增失败']);
        }

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\Customer::update(input());

        if ($model->id) {
            $model = \app\model\Customer::find($model->id);
        }

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => ['customer' => $model]]);
    }
}
